let tool = require('./tool.js');

//获取openid
export function getOpenId(data) {
  return post('/api/user/getopenid', data);
}

//通过openid登录
export function loginbyopenid(data) {
  return post('/api/Shop/loginbyopenid', data);
}

//登录/注册
export function login(data) {
  return post('/api/exam/register', data);
}
//获取详细地址
export function getnowposition(data) {
  return post('/api/Mallminiproject/getnowposition', data);
}

//获取首页
export function index(data) {
  return post('/api/Shop/index', data);
}
//课程列表
export function courselist(data) {
  return post('/api/Shop/courselist', data);
}
//获取版本
export function getVersion(data) {
  return post('/api/Shop/getVersion', data);
}
//商品详情
export function coursedetail(data) {
  return post('/api/Shop/coursedetail', data);
}
//下订单
export function placeorder(data) {
  return post('/api/Shop/placeorder', data);
}
//课程卡详情-detail
export function orderProduct(data) {
  return post('/api/Shop/orderProduct', data);
}
//激活学习卡校验手机号
export function checkMobile(data) {
  return post('/api/Shop/checkMobile', data);
}
//##教辅列表
export function teachingAids(data) {
  return post('/api/Shop/teachingAids', data);
}
//获取单词课程
export function getcourse(data) {
  return post('/api/Mallminiproject/getcourse', data);
}
//获取支付信息
export function getPayinfo(data) {
  return post('user/getpayinfo', data);
}
//支付
export function tovip(data) {
  return post('user/tovip', data);
}
//支付状态
export function orderinfo(data) {
  return post('/api/Mallminiproject/orderinfo', data);
}
// //课程商品列表
// export function courselist(data) {
//   return post('/api/Mallminiproject/courselist', data);
// }

//用户协议
export function useragreement(data) {
  return post('/api/Shop/useragreement', data);
}
//登陆绑定
export function bind(data) {
  return post('/api/Shop/bind', data);
}

//解除绑定
export function unbind(data) {
  return post('/api/Shop/unbind', data);
}
//续费
export function renewal(data) {
  return post('/api/Shop/renewal', data);
}
//获取验证码
export function sendmsg(data) {
  return post('/api/sms/sendmsg', data);
}//联系我们码
export function connectus(data) {
  return post('/api/Doubleminus/connectus', data);
}

//子账号列表
export function childlist(data) {
  return post('/api/Shop/childlist', data);
}
//获取年级
export function getgrade(data) {
  return post('/api/user/getgrade', data);
}

//查询城市 1 省 2 市 3 区县
export function getcity(data) {
  return post('/api/Shop/getcity', data);
}
//添加手机号
export function addchild(data) {
  return post('/api/Shop/addchild', data);
}
//佣金列表
export function commissionnote(data) {
  return post('/api/Shop/commissionnote', data);
} //购买记录
export function orderlist(data) {
  return post('/api/Shop/orderlist', data);
}
export function courseActivate(data) {
  return post('/api/Shop/courseActivate', data);
}


//购买记录
export function bindaccount(data) {
  return post('/api/Shop/bindaccount', data);
}
//获取分享二维码表
export function getqrcode(data) {
  return post('/api/Shop/getqrcode', data);
}

//是否购买过这个课程
export function checkorder(data) {
  return post('/api/Shop/checkorder', data);
}
//学习卡列表
export function studentcardlist(data) {
  return post('/api/Doubleminus/studentcardlist', data);
}
//学习卡详情
export function studentcardinfo(data) {
  return post('/api/Doubleminus/studentcardinfo', data);
}
//首页轮播的详情
export function carouseldetail(data) {
  return post('/api/Shop/carouseldetail', data);
}
//获取绑定的银行卡信息
export function getbankinfo(data) {
  return post('/api/Shop/getbankinfo', data);
}
//获取银行卡列表
export function getbank(data) {
  return post('/api/Shop/getbank', data);
}
//绑定银行卡
export function bindbank(data) {
  return post('/api/Shop/bindbank', data);
}
//提现
export function withdraw(data) {
  return post('/api/Shop/withdraw', data);
}
//结算记录列表
export function settlement(data) {
  return post('/api/Shop/settlement', data);
}
//get请求
export function get(...args) {
  return request('GET', ...args)
}

// post 请求
export function post(...args) {
  return request('POST', ...args)
}


// 网络请求
export function request(method, url, data) {
  if (!data) {
    data = {}
  }
  // if ($TOKEN) {
  //   data.token = $TOKEN;
  // }

  let rs = tool.getSign(data, getApp().globalData.key); //返回 bodyParam heaer sign
  console.log(rs);
  console.log(`接口: ${url} 请求参数, `, data)
  console.log("httpUtils---rs.sign=" + rs.sign);


  return new Promise((resolve, reject) => {
    wx.request({
      url: getApp().globalData.apiURL + url + '?sign=' + rs.sign,
      header: rs.header,
      method: method,
      data: data,
      success: function (res) {
        console.log(`接口: ${url} 调用成功, ${JSON.stringify(res)}`);
        wx.hideLoading()
        if (res.data.code === 1) {
          resolve(res.data)
        } else if (res.data.code == 10001) {
          wx.redirectTo({
            url: '/pages/autolog/autolog',
          })
        } else {
          setTimeout(function () {
            wx.showToast({
              title: res.data.info,
              icon: 'none',
              duration: 2000
            });
          }, 300)
        }
      },
      fail: function (err) {
        wx.hideLoading()
        wx.showModal({
          title: '温馨提示',
          content: '网络异常请稍后再试',
        })
        console.log(`接口: ${url} 调用失败, `, err)
        reject(err)
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  })
}